package ua.pp.bizon.yandex.uploader;

import ua.pp.bizon.yandex.api.IAppService;
import ua.pp.bizon.yandex.api.ObjectLookup;
import ua.pp.bizon.yandex.uploader.atom.EntryAlbum;
import ua.pp.bizon.yandex.uploader.remote.RemotePhotoCreator;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import org.apache.abdera.model.Element;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpException;

public class UploadPhoto {

	private static Log log = LogFactory.getLog(UploadPhoto.class);

	/**
	 * @deprecated
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) {
		String from = args[0];
		List<String> pathTo = new LinkedList<String>(Arrays.asList(args[1].split(":")));
		log.debug("args[0]:" + from);
		log.debug("args[1]:" + pathTo);
		String[] names = new File(from).list(new FilenameFilter() {

			@Override
			public boolean accept(File dir, String name) {
				return (name.length() < 4) ? false : name.substring(name.length() - 4).equalsIgnoreCase(".jpg") && !new File(dir, name + ".token").exists();
			}
		});
		for (int i = 0; i < names.length; i++) {
			String name = names[i];
			log.debug("start upload photo " + name);
			try {
				EntryAlbum to = ObjectLookup.lookup(IAppService.class).get().getAlbumsList().findFolder(pathTo);
				Element photo = new RemotePhotoCreator().upload(new File(from, name), to).getElement();
				FileOutputStream stream = new FileOutputStream(new File(from, name + ".token"));
				stream.write(photo.toString().getBytes());
				stream.close();
			} catch (HttpException e) {
				System.err.println(e.getMessage());
			} catch (IOException e) {
				System.err.println(e.getMessage());
			}
		}
	}

}
